﻿using System;
using System.Collections.Generic;
using System.Data;
using ContractManager.Entity;
using Speed.Data;

namespace ContractManager.BLL.Business
{
    public static class ClsTask
    {
        /// <summary>
        ///     Get Task detail by WorkflowId
        /// </summary>
        /// <param name="conDetId"></param>
        /// <returns></returns>
        public static DataTable ListDetail(Int32 conDetId)
        {
            using (Database db = Sys.NewDb())
            {
                string commandText = String.Format("EXEC dbo.usp_Task_SelectByMaster {0}", conDetId);
                return db.ExecuteDataTable(commandText);
            }
        }

        /// <summary>
        ///     Get Task Information
        /// </summary>
        
        /// <returns></returns>
        public static Task GetTask(Int32 taskId)
        {
            using (Database db = Sys.NewDb())
            {
                return db.SelectSingle<Task>(string.Format("TaskId={0}", taskId));
            }
        }

        /// <summary>
        ///     Save Change Task
        /// </summary>
        public static void SaveChange(List<Task> listAdd, List<Task> listUpdate, List<Task> listDelete)
        {
            using (Database db = Sys.NewDb())
            {
                db.BeginTransaction();

                foreach (Task addItem in listAdd)
                {
                    db.Insert<Task>(addItem);
                }
                foreach (Task updateItem in listUpdate)
                {
                    db.Update<Task>(updateItem);
                }
                foreach (Task delItem in listDelete)
                {
                    db.Delete<Task>(string.Format("TaskId={0}", delItem.TaskId));
                }

                db.Commit();
            }
        }
    }
}